package com.middlegroup.common.utils;

import org.apache.commons.codec.binary.Base64;

import java.security.*;


/**
 * @author pc
 */
public class RSAKeyPairGenerator {
	public static void main(String[] args) throws NoSuchAlgorithmException {
		// create the keys
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
		generator.initialize(1024, new SecureRandom());
		KeyPair pair = generator.generateKeyPair();
		PublicKey pubKey = pair.getPublic();
		PrivateKey privKey = pair.getPrivate();
		byte[] pk = pubKey.getEncoded();
		byte[] privk = privKey.getEncoded();
		String strpk = new String(Base64.encodeBase64(pk));
		String strprivk = new String(Base64.encodeBase64(privk));

		// System.out.println("公钥:" + Arrays.toString(pk));
		// System.out.println("私钥:" + Arrays.toString(privk));
		System.out.println("私钥Base64编码:" + strprivk);
		System.out.println("公钥Base64编码:" + strpk);

	}

}
